package com.fumin.service;

import com.fumin.common.RequestHolder;
import com.fumin.dao.SysRoleAclMapper;
import com.fumin.dao.SysRoleMapper;
import com.fumin.dao.SysRoleUserMapper;
import com.fumin.dao.SysUserMapper;
import com.fumin.exception.ParamException;
import com.fumin.model.SysRole;
import com.fumin.model.SysUser;
import com.fumin.param.RoleParam;
import com.fumin.util.BeanValidator;
import com.fumin.util.IpUtil;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author xiayuxuanmin
 * @Date 2019/11/13
 * @Description
 */
@Service
public class SysRoleService {
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysRoleUserMapper sysRoleUserMapper;
    @Resource
    private SysRoleAclMapper sysRoleAclMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysLogService sysLogService;

    public void save(RoleParam param){
        BeanValidator.check(param);
        if (checkExist(param.getName(),param.getId())) {
            throw new ParamException("角色名称已经存在!");
        }
        SysRole sysRole = SysRole.builder().name(param.getName()).type(param.getType()).status(param.getStatus())
                .remark(param.getRemark()).build();
        sysRole.setOperator(RequestHolder.getCurrentUser().getUsername());//TODO:
        sysRole.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));//TODO:
        sysRole.setOperateTime(new Date());
        sysRoleMapper.insertSelective(sysRole);
        sysLogService.saveRoleLog(null,sysRole);
    }

    public void update(RoleParam param){
        BeanValidator.check(param);
        if (checkExist(param.getName(),param.getId())) {
            throw new ParamException("角色名称已经存在!");
        }
        SysRole before = sysRoleMapper.selectByPrimaryKey(param.getId());
        Preconditions.checkNotNull(before,"待更新的角色不存在!");
        SysRole after = SysRole.builder().id(param.getId()).name(param.getName()).type(param.getType()).status(param.getStatus())
                .remark(param.getRemark()).build();
        after.setOperator(RequestHolder.getCurrentUser().getUsername());//TODO:
        after.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));//TODO:
        after.setOperateTime(new Date());
        sysRoleMapper.updateByPrimaryKeySelective(after);
        sysLogService.saveRoleLog(before,after);
    }

    public List<SysRole> getAll(){
        return sysRoleMapper.getAll();
    }

    private boolean checkExist(String name,Integer id) {
        return sysRoleMapper.countByName(name,id) > 0;
    }

    /**
     * 查找用户所有已经分配的角色
     * @param userId
     * @return
     */
    public List<SysRole> getRoleListByUserId(int userId){
        List<Integer> idList = sysRoleUserMapper.getRoleIdListByUserId(userId);
        if (CollectionUtils.isEmpty(idList)) {
            return Lists.newArrayList();
        }
        return sysRoleMapper.getByIdList(idList);
    }

    /**
     * 根据角色ID查找具有该权限点的角色集合
     * @param aclId
     * @return
     */
    public List<SysRole> getRoleListByAclId(int aclId){
        List<Integer> idList = sysRoleAclMapper.getRoleIdListByAclId(aclId);
        if (CollectionUtils.isEmpty(idList)) {
            return Lists.newArrayList();
        }
        return sysRoleMapper.getByIdList(idList);
    }

    /**
     * 查找具有指定角色的用户集合
     * @param roleList
     * @return
     */
    public List<SysUser> getUserListByRoleList(List<SysRole> roleList){
        if (CollectionUtils.isEmpty(roleList)) {
            return Lists.newArrayList();
        }
        List<Integer> roleIdList = roleList.stream().map(sysRole -> sysRole.getId()).collect(Collectors.toList());
        List<Integer> userIdList = sysRoleUserMapper.getUserIdListByRoleIdList(roleIdList);
        if (CollectionUtils.isEmpty(userIdList)) {
            return Lists.newArrayList();
        }
        return sysUserMapper.getByIdList(userIdList);
    }
}
